import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
var _excluded = ["title", "subTitle", "content", "itemTitleRender", "prefixCls", "actions", "item", "recordKey", "avatar", "cardProps", "description", "isEditable", "checkbox", "index", "selected", "loading", "expand", "onExpand", "expandable", "rowSupportExpand", "showActions", "showExtra", "type", "style", "className", "record", "onRow", "onItem", "itemHeaderRender", "cardActionProps", "extra"];
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
import { RightOutlined } from '@ant-design/icons';
import ProCard from '@ant-design/pro-card';
import { Avatar, ConfigProvider, List, Skeleton } from 'antd-v4';
import classNames from 'classnames';
import useMergedState from "rc-util/es/hooks/useMergedState";
import { useContext, useMemo } from 'react';
export function renderExpandIcon(_ref) {
  var _classNames;

  var prefixCls = _ref.prefixCls,
      _ref$expandIcon = _ref.expandIcon,
      expandIcon = _ref$expandIcon === void 0 ? _jsx(RightOutlined, {}) : _ref$expandIcon,
      onExpand = _ref.onExpand,
      expanded = _ref.expanded,
      record = _ref.record;
  var icon = expandIcon;
  var expandClassName = "".concat(prefixCls, "-row-expand-icon");

  var onClick = function onClick(event) {
    onExpand(!expanded);
    event.stopPropagation();
  };

  if (typeof expandIcon === 'function') {
    icon = expandIcon({
      expanded: expanded,
      onExpand: onExpand,
      record: record
    });
  }

  return _jsx("span", {
    className: classNames(expandClassName, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-row-expanded"), expanded), _defineProperty(_classNames, "".concat(prefixCls, "-row-collapsed"), !expanded), _classNames)),
    onClick: onClick,
    children: icon
  });
}

function ProListItem(props) {
  var _classNames2, _ref3, _classNames4, _ref4, _classNames6;

  var customizePrefixCls = props.prefixCls;

  var _useContext = useContext(ConfigProvider.ConfigContext),
      getPrefixCls = _useContext.getPrefixCls;

  var prefixCls = getPrefixCls('pro-list', customizePrefixCls);
  var defaultClassName = "".concat(prefixCls, "-row");

  var title = props.title,
      subTitle = props.subTitle,
      content = props.content,
      itemTitleRender = props.itemTitleRender,
      restPrefixCls = props.prefixCls,
      actions = props.actions,
      item = props.item,
      recordKey = props.recordKey,
      avatar = props.avatar,
      cardProps = props.cardProps,
      description = props.description,
      isEditable = props.isEditable,
      checkbox = props.checkbox,
      index = props.index,
      selected = props.selected,
      loading = props.loading,
      propsExpand = props.expand,
      propsOnExpand = props.onExpand,
      expandableConfig = props.expandable,
      rowSupportExpand = props.rowSupportExpand,
      showActions = props.showActions,
      showExtra = props.showExtra,
      type = props.type,
      style = props.style,
      _props$className = props.className,
      propsClassName = _props$className === void 0 ? defaultClassName : _props$className,
      record = props.record,
      onRow = props.onRow,
      onItem = props.onItem,
      itemHeaderRender = props.itemHeaderRender,
      cardActionProps = props.cardActionProps,
      extra = props.extra,
      rest = _objectWithoutProperties(props, _excluded);

  var _ref2 = expandableConfig || {},
      expandedRowRender = _ref2.expandedRowRender,
      expandIcon = _ref2.expandIcon,
      expandRowByClick = _ref2.expandRowByClick,
      _ref2$indentSize = _ref2.indentSize,
      indentSize = _ref2$indentSize === void 0 ? 8 : _ref2$indentSize,
      expandedRowClassName = _ref2.expandedRowClassName;

  var _useMergedState = useMergedState(!!propsExpand, {
    value: propsExpand,
    onChange: propsOnExpand
  }),
      _useMergedState2 = _slicedToArray(_useMergedState, 2),
      expanded = _useMergedState2[0],
      onExpand = _useMergedState2[1];

  var className = classNames((_classNames2 = {}, _defineProperty(_classNames2, "".concat(defaultClassName, "-selected"), !cardProps && selected), _defineProperty(_classNames2, "".concat(defaultClassName, "-show-action-hover"), showActions === 'hover'), _defineProperty(_classNames2, "".concat(defaultClassName, "-type-").concat(type), !!type), _defineProperty(_classNames2, "".concat(defaultClassName, "-editable"), isEditable), _defineProperty(_classNames2, "".concat(defaultClassName, "-show-extra-hover"), showExtra === 'hover'), _classNames2), defaultClassName);
  var extraClassName = classNames(_defineProperty({}, "".concat(propsClassName, "-extra"), showExtra === 'hover'));
  var needExpanded = expanded || Object.values(expandableConfig || {}).length === 0;
  var expandedRowDom = expandedRowRender && expandedRowRender(record, index, indentSize, expanded);
  var extraDom = useMemo(function () {
    if (!actions || cardActionProps === 'actions') {
      return undefined;
    }

    return [_jsx("div", {
      onClick: function onClick(e) {
        return e.stopPropagation();
      },
      children: actions
    }, "action")];
  }, [actions, cardActionProps]);
  var actionsDom = useMemo(function () {
    if (!actions || !cardActionProps || cardActionProps === 'extra') {
      return undefined;
    }

    return [_jsx("div", {
      onClick: function onClick(e) {
        return e.stopPropagation();
      },
      children: actions
    }, "action")];
  }, [actions, cardActionProps]);
  var titleDom = title || subTitle ? _jsxs("div", {
    className: "".concat(className, "-header-title"),
    children: [title && _jsx("div", {
      className: "".concat(className, "-title"),
      children: title
    }), subTitle && _jsx("div", {
      className: "".concat(className, "-subTitle"),
      children: subTitle
    })]
  }) : null;
  var metaTitle = (_ref3 = itemTitleRender && (itemTitleRender === null || itemTitleRender === void 0 ? void 0 : itemTitleRender(record, index, titleDom))) !== null && _ref3 !== void 0 ? _ref3 : titleDom;
  var metaDom = metaTitle || avatar || subTitle || description ? _jsx(List.Item.Meta, {
    avatar: avatar,
    title: metaTitle,
    description: description && needExpanded && _jsx("div", {
      className: "".concat(className, "-description"),
      children: description
    })
  }) : null;
  var rowClassName = classNames((_classNames4 = {}, _defineProperty(_classNames4, "".concat(className, "-item-has-checkbox"), checkbox), _defineProperty(_classNames4, "".concat(className, "-item-has-avatar"), avatar), _defineProperty(_classNames4, className, className), _classNames4));
  var cardTitleDom = useMemo(function () {
    if (avatar || title) {
      return _jsxs(_Fragment, {
        children: [avatar && _jsx(Avatar, {
          size: 22,
          src: avatar,
          className: getPrefixCls('list-item-meta-avatar')
        }), _jsx("span", {
          className: getPrefixCls('list-item-meta-title'),
          children: title
        })]
      });
    }

    return null;
  }, [avatar, getPrefixCls, title]);
  var defaultDom = !cardProps ? _jsx(List.Item, _objectSpread(_objectSpread(_objectSpread(_objectSpread({
    className: classNames(rowClassName, _defineProperty({}, propsClassName, propsClassName !== defaultClassName))
  }, rest), {}, {
    actions: extraDom,
    extra: !!extra && _jsx("div", {
      className: extraClassName,
      children: extra
    })
  }, onRow === null || onRow === void 0 ? void 0 : onRow(record, index)), onItem === null || onItem === void 0 ? void 0 : onItem(record, index)), {}, {
    onClick: function onClick(e) {
      var _onRow, _onRow$onClick, _onItem, _onItem$onClick;

      onRow === null || onRow === void 0 ? void 0 : (_onRow = onRow(record, index)) === null || _onRow === void 0 ? void 0 : (_onRow$onClick = _onRow.onClick) === null || _onRow$onClick === void 0 ? void 0 : _onRow$onClick.call(_onRow, e);
      onItem === null || onItem === void 0 ? void 0 : (_onItem = onItem(record, index)) === null || _onItem === void 0 ? void 0 : (_onItem$onClick = _onItem.onClick) === null || _onItem$onClick === void 0 ? void 0 : _onItem$onClick.call(_onItem, e);

      if (expandRowByClick) {
        onExpand(!expanded);
      }
    },
    children: _jsxs(Skeleton, {
      avatar: true,
      title: false,
      loading: loading,
      active: true,
      children: [_jsxs("div", {
        className: "".concat(className, "-header"),
        children: [_jsxs("div", {
          className: "".concat(className, "-header-option"),
          children: [!!checkbox && _jsx("div", {
            className: "".concat(className, "-checkbox"),
            children: checkbox
          }), Object.values(expandableConfig || {}).length > 0 && rowSupportExpand && renderExpandIcon({
            prefixCls: prefixCls,
            expandIcon: expandIcon,
            onExpand: onExpand,
            expanded: expanded,
            record: record
          })]
        }), (_ref4 = itemHeaderRender && (itemHeaderRender === null || itemHeaderRender === void 0 ? void 0 : itemHeaderRender(record, index, metaDom))) !== null && _ref4 !== void 0 ? _ref4 : metaDom]
      }), needExpanded && (content || expandedRowDom) && _jsxs("div", {
        className: "".concat(className, "-content"),
        children: [content, expandedRowRender && rowSupportExpand && _jsx("div", {
          className: expandedRowClassName && expandedRowClassName(record, index, indentSize),
          children: expandedRowDom
        })]
      })]
    })
  })) : _jsx(ProCard, _objectSpread(_objectSpread(_objectSpread({
    bordered: true,
    loading: loading,
    hoverable: true
  }, cardProps), {}, {
    title: cardTitleDom,
    subTitle: subTitle,
    extra: extraDom,
    actions: actionsDom,
    bodyStyle: _objectSpread({
      padding: 24
    }, cardProps.bodyStyle)
  }, onItem === null || onItem === void 0 ? void 0 : onItem(record, index)), {}, {
    children: _jsx(Skeleton, {
      avatar: true,
      title: false,
      loading: loading,
      active: true,
      children: _jsxs("div", {
        className: "".concat(className, "-header"),
        children: [itemTitleRender && (itemTitleRender === null || itemTitleRender === void 0 ? void 0 : itemTitleRender(record, index, titleDom)), content]
      })
    })
  }));

  if (!cardProps) {
    return defaultDom;
  }

  return _jsx("div", {
    className: classNames((_classNames6 = {}, _defineProperty(_classNames6, "".concat(className, "-card"), cardProps), _defineProperty(_classNames6, propsClassName, propsClassName !== defaultClassName), _classNames6)),
    style: style,
    children: defaultDom
  });
}

export default ProListItem;